using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Bubble : Singleton<Bubble>
{
    public Camera uiCamera;
    public RectTransform parent;
    public RectTransform taskRequirement;
    public RectTransform chestReward;
    public RectTransform iconDescribe;
    public RectTransform talentDetail;

    public float posYUp = 0;

    //ObjectPool<GameObject> bubblePool;
    public List<GameObject> bubbleList;

    // Start is called before the first frame update
    void Start()
    {
        //bubblePool = new ObjectPool<GameObject>(CreatePooledItem,)
        bubbleList = new();
    }

    public GameObject CreateBubble(InitBubbleType type, Vector3 wpos, bool state = true)
    {
        var screenPos = RectTransformUtility.WorldToScreenPoint(uiCamera, wpos);
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, screenPos, uiCamera, out var pos);
        GameObject go = null;
        if (type == InitBubbleType.taskRequirement)
        {
            go = GameObject.Instantiate(taskRequirement).gameObject;
            var trans = taskRequirement.GetComponent<RectTransform>();
            go.GetComponent<RectTransform>().sizeDelta = new Vector2(trans.rect.width, trans.rect.height);
            posYUp = 25;
            LayoutRebuilder.ForceRebuildLayoutImmediate(go.transform.Find("Content").GetComponent<RectTransform>());
        }
        else if (type == InitBubbleType.chestReward)
        {
            go = GameObject.Instantiate(chestReward).gameObject;
            var trans = chestReward.GetComponent<RectTransform>();
            go.GetComponent<RectTransform>().sizeDelta = new Vector2(trans.rect.width, trans.rect.height);
            posYUp = 20;
        }
        else if (type == InitBubbleType.iconDescribe)
        {
            go = GameObject.Instantiate(iconDescribe).gameObject;
            var trans = iconDescribe.GetComponent<RectTransform>();
            go.GetComponent<RectTransform>().sizeDelta = new Vector2(trans.rect.width, trans.rect.height);
            posYUp = 40;
        }
        else
        {
            go = GameObject.Instantiate(talentDetail).gameObject;
            var trans = talentDetail.GetComponent<RectTransform>();
            go.GetComponent<RectTransform>().sizeDelta = new Vector2(trans.rect.width, trans.rect.height);
            posYUp = 140;
        }

        go.transform.SetParent(parent);
        go.transform.localPosition = new Vector3(pos.x, pos.y + posYUp, 0);
        if (type == InitBubbleType.talentDetail)
        {
            if (state)
            {
                go.transform.localPosition = new Vector3(pos.x - 70, pos.y + posYUp, 0);
            }
            else
            {
                go.transform.localPosition = new Vector3(pos.x - 70, pos.y + posYUp + 100, 0);
            }

        }
        go.transform.localScale = Vector3.one;

        bubbleList.Add(go);
        return go;
    }

    public void FixPos(GameObject go)
    {
        var childRectPos = go.transform.GetChild(0).GetComponent<RectTransform>();
        LayoutRebuilder.ForceRebuildLayoutImmediate(childRectPos);
        go.transform.localScale = Vector3.one;

        var contentPos = Vector3.zero;
        contentPos.y = 60;
        var pos = go.transform.localPosition;
        // parent的pivot为0
        var selfRect = childRectPos.rect;
        // 左右贴边
        if (pos.x - selfRect.width * 0.5f < 0)
        {
            contentPos.x += (pos.x - selfRect.width * 0.5f);
        }

        if (pos.x + selfRect.width * 0.5f > parent.rect.width)
        {
            contentPos.x -= (pos.x + selfRect.width * 0.5f) - parent.rect.width;
        }

        childRectPos.localPosition = contentPos;
    }

    public GameObject CreateTlentBubble(RectTransform rect, Vector3 wpos)
    {
        var screenPos = RectTransformUtility.WorldToScreenPoint(uiCamera, wpos);
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, screenPos, uiCamera, out var pos);
        GameObject go = GameObject.Instantiate(talentDetail).gameObject;
        var trans = talentDetail.GetComponent<RectTransform>();
        go.GetComponent<RectTransform>().sizeDelta = new Vector2(trans.rect.width, trans.rect.height);
        posYUp = rect.rect.height + 30;

        go.transform.SetParent(parent);

        var goRect = rect.rect;
        pos.x -= goRect.width * 0.5f;
        pos.y += goRect.height;


        // parent的pivot为0
        var talentRect = talentDetail.rect;
        // 左右贴边
        if (pos.x - talentRect.width * 0.5f < 0)
        {
            pos.x += -(pos.x - talentRect.width * 0.5f);
        }

        if (pos.x + talentRect.width * 0.5f > parent.rect.width)
        {
            pos.x -= (pos.x + talentRect.width * 0.5f) - parent.rect.width;
        }
        
        go.transform.localPosition = pos;
        
        go.transform.localScale = Vector3.one;

        bubbleList.Add(go);
        return go;
    }

    public void RemoveBubble(GameObject obj)
    {
        //obj.SetActive(false);
        bubbleList.Remove(obj);
        Destroy(obj);
    }

    public void RemoveAllBubble(Vector2 value)
    {
        if (bubbleList.Count == 0)
        {
            return;
        }

        for (int i = bubbleList.Count - 1; i >= 0; i--)
        {
            var obj = bubbleList[i];
            bubbleList.RemoveAt(i);
            Destroy(obj);
        }
    }
}

public enum InitBubbleType
{
    taskRequirement,
    chestReward,
    iconDescribe,
    talentDetail,
}
